package com.example.myapp.ADO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SQLDAO {

    private SQLiteDatabase db;
    private Context context;

    public SQLDAO(Context context) {
        SQLitedb dbHelper = new SQLitedb(context, 1);
        db = dbHelper.getReadableDatabase();
    }

    ///插入数据
    public void InsertDate(String userid,int msgcount) {

        ContentValues values = new ContentValues();
        values.put("msgcount",msgcount);
        values.put("keys",userid);
        //返回插入的id
        long id = db.insert("msgs",null,values);
        //3.关闭连接
        if (id>0){
            Log.w("aaaa","插入数据库成功");

        }else {
            Log.w("aaaa","插入数据库失败");
        }


    }

    public void upDate( String  userid  ,int msgcount){
        ContentValues values = new ContentValues();
        values.put("msgcount",msgcount);
        //返回插入的id
        int row  = db.update("msgs",values,"keys=?",new String []{userid} );
      if (row>0){
          Log.w("aaaa","更新数据库成功");
      }else {
          Log.w("aaaa","更新数据库失败");
      }
    }


    public int getMsgcount(String userid){
        int sth =0;
        Cursor sum =db.rawQuery("select msgcount FROM msgs WHERE keys='"+userid+"'", null);
        if (sum.moveToNext()) { //定位到下一个
            sth =sum.getInt(0);
        }
        return sth;
    }

    public void addCount(String userid,int num) {
        int sth = 0;
        Cursor sum = db.rawQuery("select msgcount FROM msgs WHERE keys='" + userid + "'", null);
        if (sum.moveToNext()) { //定位到下一个
            sth = sum.getInt(0);
        }
        sth = sth + num;
        ContentValues values = new ContentValues();
        values.put("msgcount", sth);
        //返回插入的id
        int row = db.update("msgs", values, "keys=?", new String[]{userid});
        if (row > 0) {
            Log.w("aaaa", "添加数据成功");
        } else {
            Log.w("aaaa", "添加数据失败");
        }

    }


}
